var Message = function() {
	var _MessageTpl = '<table class="popup-dialog" cellspacing="0" cellpadding="0" border="0">'
	    + '<tbody>'
	    + '<tr>'
	    + '  <td class="top_l"></td><td class="top_c"></td><td class="top_r"></td>'
	    + '</tr>'
	    + '<tr>'
	    + '  <td class="mid_l"></td><td class="mid_c"><div class="popup-inner" style="width: 100%"><div class="noty_message"><span class="noty_text"></span><div class="noty_close">×</div></div></div></td><td class="mid_r"></td>'
	    + '</tr>'
	    + '<tr>'
	    + '  <td class="bottom_l"></td><td class="bottom_c"></td><td class="bottom_r"></td>'
	    + '</tr>'
	    + '</tbody></table>';
	    
	var dynamicMessageHideTimeout = null;
	
	var loadingInfoId = null;
	
	function showDynamicMessage(target, msg, isSuccess, waitseconds,position) {
		if(!document.getElementById('dynamic-message-div')) {
			var _dialogInput = '<div id="dynamic-message-div"></div>';
			jQuery('body').append(_dialogInput);
		}
		if(isSuccess === true) {
			jQuery('#dynamic-message-div').attr('class', 'alert alert-success').css('background-image', '');
		} else if(isSuccess === false){
			jQuery('#dynamic-message-div').attr('class', 'alert alert-error').css('background-image', '');
		} else{
			jQuery('#dynamic-message-div').attr('class', 'alert alert-success').css('background-image', 'none');
		}
		jQuery('#dynamic-message-div').html(msg);
		var left, top;
		if(position) {
			left = position.left ? position.left : 0;
			top = position.top ? position.top : 0;
		}else{
			if(jQuery(target).length == 0) {
				return;
			}
			var position = jQuery(target).offset();
			left = position.left + jQuery(target).width() + 5;
			top = position.top + jQuery(target).height() + 5;
			var messageWidth =  jQuery('#dynamic-message-div').outerWidth();
			var messageHeight =  jQuery('#dynamic-message-div').height();
			//超过Body的显示范围，则显示在左侧
			if((left + messageWidth) > jQuery('body').width()) {
				left = left - messageWidth;
			}else{
				left = position.left;
			}
			//如果太靠下的话则显示在上面
			if((top + messageHeight) > jQuery('body').height()) {
				top = top - messageHeight - 10;
			}
		}
		jQuery('#dynamic-message-div').css('left', left + 'px').css('top', top + 'px');
		jQuery('#dynamic-message-div').show();
		
		if(dynamicMessageHideTimeout) {
			clearTimeout(dynamicMessageHideTimeout);
		}
		var waitmilliseconds = (waitseconds || 2) * 1000;
		if (waitmilliseconds > 0) {
			dynamicMessageHideTimeout = setTimeout(function(){
				jQuery("#dynamic-message-div").fadeOut(800);
			}, waitmilliseconds);
		}
	}
	return {
		/** 显示成功信息 */
		success : function(target, msg, seconds,position) {
			if(arguments.length == 1 || (arguments.length == 2 && typeof arguments[1] == 'number')) {
				var successMsg = arguments[0];
				var timer = arguments[1];
				var options = {
					content: successMsg,
					icon: 'succeed',
					fixed : true,
					time: timer
				};
				if(timer && timer <= 5) {
					options.title = false;
					options.cancel = false;
				}else{
					options.button = [{
					    name: '确定',
					    focus: true
					}];
				}
				art.dialog(options);
				return;
			}
			showDynamicMessage(target, msg, true, seconds,position);
		},
		error : function(target, msg, seconds, isclose,position) {
			if(arguments.length == 1 || (arguments.length == 2 && typeof arguments[1] == 'number')) {
				seconds = (arguments.length == 2 && typeof arguments[1] == 'number')? arguments[1] : null;
				var options = {
					content: arguments[0],
					icon: 'error',
					fixed : true,
					time: seconds
				};
				if(seconds && seconds <= 5) {
					options.title = false;
					options.cancel = false;
				}else{
					options.button = [{
					    name: '确定',
					    focus: true
					}];
				}
				art.dialog(options);
				return;
			}
			if(isclose) {
				msg = '<div class="alignright clear"><span onclick="Message.hide()" style="vertical-align: middle; color: gray; cursor: pointer;display: inline-block;zoom:1;*display:inline;">X</span></div>' + msg;
			}
			showDynamicMessage(target, msg, false, seconds,position);
		},
		waiting : function(targetid, msg){
			if(arguments.length == 1) {
				//中部显示载入信息
				loadingInfoId = Message.info({ 
					text: '<span class="waiting-icon"></span>\t' + arguments[0], 
					layout: 'center',
					autoClose: false,
					showCloseBtn: false,
					closeOnSelfClick: false,
					modal: true
				});
				jQuery('#' + loadingInfoId + ' .noty_message').attr('style', 'padding-left:15px !important;background-image: none');
			}else{
				showDynamicMessage(targetid, '<span class="waiting-icon"></span>\t' + msg, null, -1);
			}
		},
		hide : function(){
			if (document.getElementById('dynamic-message-div')) {
				document.getElementById('dynamic-message-div').style.display = 'none';
			}
			if(loadingInfoId) {
				jQuery.noty.close(loadingInfoId);
				loadingInfoId = null;
			}
		},
		/** 更新innerhtml的内容来显示等待 */
		innerwaiting : function(targetid, msg){
			document.getElementById(targetid).style.display = '';
			document.getElementById(targetid).innerHTML = '<span class="waiting-icon"></span>\t' + msg;
		},
		
		info: function(options) {
			seajs.use('ui.noty', function(){
				if(typeof options == 'string') {
					options = { text: options };
				}
				var type = 'information';
				if(options.error) {
					type = 'error';
				}else if(options.warning) {
					type = 'alert';
				}else if(options.success) {
					type = 'success';
				}
				if(options.autoClose === false) {
					options.timeout = 0;
				}else if(typeof options.timeout == 'undefined'){
					//默认5秒后消失
					options.timeout = 5;
				}
				jQuery.noty.defaultOptions.theme = 'noty_theme_twitter';
				jQuery.noty.defaultOptions.template = _MessageTpl;
	  			return noty({
					text: options.text,
					layout: options.layout || 'topRight',
					type: type,
					modal: options.modal,
					timeout: options.timeout * 1000,
					closeButton: (false == options.showCloseBtn? false : true),
					closeOnSelfClick: (false == options.closeOnSelfClick? false : true),
					force: true
				});
			});
		},
		/**
		 * 确认对话框
		 * 
		 * @param msg
		 *            提示信息
		 * @param func
		 *            如果是function的话，则默认有两个按钮：确定和关闭，“确定”执行这个函数
		 * @returns
		 */
		confirm: function(msg, func) {
			art.dialog({
				content: msg,
				fixed : true,
				ok:function(){
					func();
				},
				cancelVal:'取消',
				cancel:true,
				lock: true,
				icon: 'question'
			});
		},
		// 页面级waiting
		showWaiting:function(msg){
			$('body > .isloading-overlay').remove();
            $('body').isLoading({text : msg?msg:'加载中...'});
		},
		hideWaiting:function(){
			$('body').isLoading('hide');
		},
		// 元素内waiting
		innerWaiting:function(element, msg){
			$(element).isLoading({text : msg?msg:'加载中...', position:'inside'});
		},
		hideInnerWaiting:function(element){
			$(element).isLoading('hide');
		}				
	};
}();
